package com.minelx.parallel;

import java.util.Iterator;
import java.util.List;

public class ListPartitioner<T> implements Iterator<List<T>> {

	private final List<T> items;

	private final int itemsCountOfTask;

	private int index;

	public ListPartitioner(List<T> items, int itemsCountOfTask) {
		this.items = items;
		this.itemsCountOfTask = itemsCountOfTask;
	}

	@Override
	public synchronized boolean hasNext() {
		return index < items.size();
	}

	@Override
	public synchronized List<T> next() {
		if (!hasNext()) {
			throw new IllegalStateException("there is no item to fetch.");
		}
		List<T> result = items.subList(index, index + itemsCountOfTask);
		index += itemsCountOfTask;
		return result;
	}
}
